import java.awt.Color;
import java.awt.Dimension;
import java.text.NumberFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class DeductionsForm extends JPanel{
	private static final long serialVersionUID = 1L;
	private JLabel ulabel,userID,sdlabel,curDeduc, adlabel, relabel;
	private JTextField newDeduc, otherReason;
	private JRadioButton loan,other;

	public DeductionsForm(){
		ulabel = new JLabel ("UserID:");
		userID = new JLabel ("");
		sdlabel = new JLabel ("Current salary deductions:");
		curDeduc = new JLabel("");
		adlabel = new JLabel ("Additional deductions:");
		newDeduc = new JTextField (5);
		loan = new JRadioButton ("Loans");
		other = new JRadioButton ("Others, specify:");
		relabel = new JLabel ("Reason:");
		otherReason = new JTextField (5);

		//adjust size and set layout
		setPreferredSize (new Dimension (434, 304));
		setLayout (null);

		//add components

		add (ulabel);
		add (userID);
		add (sdlabel);
		add (curDeduc);
		add (adlabel);
		add (newDeduc);
		add (loan);
		add (other);
		add (relabel);
		add (otherReason);

		//set component bounds (only needed by Absolute Positioning)

		ulabel.setBounds (25, 65, 55, 25);
		userID.setBounds (70, 65, 100, 25);
		sdlabel.setBounds (25, 95, 160, 25);
		curDeduc.setBounds (180, 95, 100, 25);
		adlabel.setBounds (25, 125, 130, 25);
		newDeduc.setBounds (160, 125, 100, 25);
		loan.setBounds (25, 175, 70, 25);
		other.setBounds (25, 200, 115, 25);
		relabel.setBounds (25, 150, 100, 25);
		otherReason.setBounds (145, 200, 100, 25);

		setSize(440,350);
		setOpaque(false);
		setLayout(null);

		ulabel.setForeground(Color.WHITE);
		userID.setForeground(Color.WHITE);
		sdlabel.setForeground(Color.WHITE);
		curDeduc.setForeground(Color.WHITE);
		adlabel.setForeground(Color.WHITE);
		relabel.setForeground(Color.WHITE);
		loan.setForeground(Color.WHITE);
		other.setForeground(Color.WHITE);

	}



	public String getCurDeduc() {
		return curDeduc.getText().trim();
	}

	public void setCurDeduc(String dbCur){
		curDeduc.setText(dbCur);
	}

	public void setuID(int UID){
		userID.setText(String.valueOf(UID));
	}

	public String getNewDeduc(){
		String amount="";
		float amtloan;
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);            
		nf.setGroupingUsed(false); 
		if(loan.isSelected()){
			amtloan = Integer.parseInt(newDeduc.getText().trim());
			amtloan = amtloan/24;
			amount = String.valueOf(nf.format(amtloan)); //rounds float value to 2 decimal places
		}
		else if(other.isSelected())
			amount = newDeduc.getText().trim();
		return amount;
	}

	public String getDeducReason(){
		String reason = "";
		if(loan.isSelected())
			reason = "Salary Loan";
		else if(other.isSelected()){
			if(otherReason.getText().trim().length() == 0)
				reason = "inc";
			else
				reason = otherReason.getText().trim();
		}
		else
			reason = "invalid";
		return reason;
	}

}